import { loadScript } from '../methods'

/**
 * @class babylon 锅炉模型
 */
class BabylonBoiler {
  constructor() {
    this._assets = [
      'http://192.168.139.84:8092/babylon/babylon-boiler-origin/0.0.1/index.umd.js',
    ]
    this._promise = null
    this._babylonBoiler = null
  }

  init() {
    if (!this._promise) {
      this._promise = new Promise((resolve, reject) => {
        if (this._babylonBoiler) {
          resolve(this._babylonBoiler)
          return
        }

        loadScript(this._assets, { async: true, defer: true }).then(() => {
          this._babylonBoiler = window.babylonBoilerOrigin

          resolve({
            babylonBoiler: this._babylonBoiler,
          })
        }).catch(() => {
          this._promise = null
          reject(new Error('babylon 锅炉模型不可用'))
        })
      })
    }

    return this._promise
  }

  /**
   * init的简化版本
   * @param {Function} resolve
   * @param {Function} reject
   * @return {Promise}
   */
  ready(resolve, reject) {
    return this.init().then(resolve).catch(reject)
  }
}

export default new BabylonBoiler()
